package game.model.impl

import game.Config
import game.business.AttackAble
import game.business.AutoMove
import game.business.DestroyAble
import game.business.SufferAble
import game.enums.Direction
import game.model.View
import org.itheima.kotlin.game.core.Painter

/**
 * 子弹
 */
class Bullet(override var x: Int, override var y: Int,
             override val  currentDirection: Direction, override val source: View)
            : AutoMove , DestroyAble,AttackAble,SufferAble{


    override val blood: Int = 1

    override val speed: Int = Config.bulletSpeed
    override var width:Int = Config.block
    override var height: Int= Config.block

    init {
        this.width = when(currentDirection){
            Direction.up,Direction.down -> 16
            Direction.right,Direction.left -> 32
            else -> Config.block
        }
        this.height = when(currentDirection){
            Direction.up,Direction.down -> 32
            Direction.right,Direction.left -> 16
            else -> Config.block
        }

    }

    override val attackPower: Int = Config.BulletPower

    private var isDestroyed = false

    override fun draw() {
        val imgPath =
                when(currentDirection){
                    Direction.up -> "img/shot_bottom.gif"
                    Direction.down -> "img/shot_top.gif"
                    Direction.left -> "img/shot_right.gif"
                    Direction.right -> "img/shot_left.gif"
                }
       Painter.drawImage(imgPath,x,y)
    }

    override fun autoMove() {
        //根据自己的方法，来改变自己的x和y
        when(currentDirection){
            Direction.up -> y -= speed
            Direction.down -> y += speed
            Direction.left -> x -= speed
            Direction.right -> x += speed
        }
    }

    override fun isDestroyed() : Boolean {
        if (isDestroyed) return true

        //当子弹离开了屏幕后，需要被销毁
        if (x <- width)return true
        if (x > Config.gameWidth)return true
        if (y <- height)return true
        if (y > Config.gameHeight)return true
        return false
    }



    override fun isCollision(sufferAble: SufferAble): Boolean {
        return checkCollision(sufferAble)
    }

    override fun notifyAttack(sufferAble: SufferAble) {
        //子弹命中 ， 需要被销毁
        isDestroyed = true
    }

    override fun notifySuffer(attackAble: AttackAble): Array<View>? {
               return arrayOf(Blast(x,y))
    }
}